package login;
/**
 * @describe 物业管理系统登录窗口（实现登录功能，连接注册窗口）
 * 实现细则（1、通过与数据库中用户信息的匹配实现登录
 * 			2、身份为物业可以登录物业窗口和住户窗口，身份为住户只可以登录住户窗口
 * 
 */


import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;

import UserDatabase.UserDatabase;
import function.PropertyFrame;
import function.UserFrame;
import register.RegisterFrame;


public class LoginFrame extends JFrame{
	
	//组件
	private JLabel l_title;//标题
	private JLabel l_name,l_password,l_type;//姓名，密码，身份
	private JTextField t_name;//姓名输入框
	private JPasswordField p_password;//密码输入框
	private JComboBox<String> c_type;//身份下拉框
	private JButton b_login,b_register;//登录按钮，注册按钮
	private String Username;
	
	//结构化，生成窗体
	public LoginFrame() {
		this.setSize(400,500);
		this.setTitle("物业管理系统");
		this.setLocation(500,150);
		init();//初始化组件
		this.setVisible(true);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}
	
	//实现初始化
	public void init() {
		l_title = new JLabel("欢迎使用物业管理系统",JLabel.CENTER);
		Font fontBB = new Font("黑体",Font.BOLD,30);
		Font fontSB = new Font("宋体",Font.BOLD,16);
		l_title.setFont(fontBB);
		l_name = new JLabel("姓名",JLabel.CENTER);
		
		l_name.setFont(fontSB);
		l_password = new JLabel("密码",JLabel.CENTER);
		l_password.setFont(fontSB);
		l_type = new JLabel("身份",JLabel.CENTER);
		l_type.setFont(fontSB);
		
		t_name = new JTextField();
		t_name.setFont(fontSB);
		p_password = new JPasswordField();
		p_password.setFont(fontSB);
		c_type = new JComboBox<String>();
		c_type.addItem("业主");
		c_type.addItem("物业");
		c_type.setFont(fontSB);
		
		b_login = new JButton("登录");
		b_login.setFont(fontSB);
		b_login.addActionListener(new ActionListener () {
			
			public void actionPerformed(ActionEvent e) {
				//连接数据库
				UserDatabase udb = new UserDatabase(); 
				//连接个人中心
				String uname = t_name.getText();
				Username = uname;
				
				char ch[] = p_password.getPassword();
				String upassword = new String(ch);
				JFrame f = new JFrame();
				Object type = c_type.getSelectedItem();
				if(uname.equals("")) {
					JOptionPane.showMessageDialog(f,"姓名不能为空！","提示框",JOptionPane.WARNING_MESSAGE);	
				}else if(upassword.equals("")) {
					JOptionPane.showMessageDialog(f,"密码不能为空！","提示框",JOptionPane.WARNING_MESSAGE);
				}else if(!udb.match(uname, upassword)){
					JOptionPane.showMessageDialog(f,"姓名或密码错误！","提示框",JOptionPane.WARNING_MESSAGE);
				}else {
					if(type.equals("物业")&&(udb.matchtype(uname, type))) {
						new PropertyFrame();
						setVisible(false);
					}else if(type.equals("物业")&&!(udb.matchtype(uname, type))) {
						JOptionPane.showMessageDialog(f,"身份错误！","提示框",JOptionPane.WARNING_MESSAGE);
					}else if(type.equals("业主")) {
						new UserFrame(Username);
						setVisible(false);
					}
				}
			}
		});
		
		b_register = new JButton("注册");
		b_register.addActionListener(new ActionListener() {
			
			public void actionPerformed(ActionEvent e) {
				new RegisterFrame();
				setVisible(false);
			}
			
		});
		b_register.setFont(fontSB);
		
		this.setLayout(null);
		JPanel p = new JPanel();
		p.setLayout(new GridLayout(1,1,5,5));
		p.add(l_title);
		p.setBounds(20,4,350,100);
		this.add(p);
		
		p = new JPanel();
		p.setLayout(new GridLayout(4,2,5,5));
		p.add(l_name);
		p.add(t_name);
		p.add(l_password);
		p.add(p_password);
		p.add(l_type);
		p.add(c_type);
		p.setBounds(7,140,350,250);
		
		this.add(p);
		
		p = new JPanel();
		p.setLayout(new GridLayout(1,2,5,5));
		p.add(b_login);
		p.add(b_register);
		p.setBounds(19,390,350,50);
		
		this.add(p);
		this.setResizable(false);
	}
	
}
